<template>
  <div class="custom-table-wrapper">
    <!-- 表格容器，支持 X 轴滚动 -->
    <div class="table-scroll-container">
      <!-- 表格头部 -->
      <div class="table-header">
        <div v-for="column in columns" :key="column.prop" class="header-cell"
          :style="{ width: column.width || 'auto', textAlign: column.align || 'left' }">
          {{ column.label }}
        </div>
      </div>

      <!-- 表格内容 -->
      <div class="table-body">
        <div v-if="loading" class="loading-wrapper">
          <div class="loading-spinner"></div>
          <div class="loading-text">加载中...</div>
        </div>
        <template v-else>
          <div v-for="(row, index) in data" :key="index" class="table-row" :class="{ 'row-hover': hoverRow === index }"
            @mouseenter="hoverRow = index" @mouseleave="hoverRow = -1" @dblclick="showDetail(row, index)">
            <div v-for="column in columns" :key="column.prop" class="table-cell"
              :style="{ width: column.width || 'auto', textAlign: column.align || 'left' }">
              <slot :name="column.prop" :row="row" :index="index" :column="column">
                {{ row[column.prop] }}
              </slot>
            </div>
          </div>
        </template>
      </div>
    </div>

    <!-- 详情弹出层 -->
    <div v-if="showDetailModal" class="detail-modal-overlay" @click="closeDetail">
      <div class="detail-modal" @click.stop>
        <div class="detail-header">
          <h3>详细信息</h3>
          <button class="close-btn" @click="closeDetail">×</button>
        </div>
        <div class="detail-content">
          <slot name="detail" :row="selectedRow" :index="selectedIndex">
            <!-- 默认详情显示 -->
            <div class="default-detail">
              <div v-for="column in columns" :key="column.prop" class="detail-item">
                <span class="detail-label">{{ column.label }}:</span>
                <span class="detail-value">{{ selectedRow[column.prop] }}</span>
              </div>
            </div>
          </slot>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';

// 定义组件属性
const props = defineProps({
  // 表格数据
  data: {
    type: Array,
    default: () => []
  },
  // 列配置
  columns: {
    type: Array,
    default: () => []
  },
  // 是否显示loading状态
  loading: {
    type: Boolean,
    default: false
  }
});

// 响应式数据
const hoverRow = ref(-1);
const showDetailModal = ref(false);
const selectedRow = ref(null);
const selectedIndex = ref(-1);

// 显示详情
const showDetail = (row, index) => {
  selectedRow.value = row;
  selectedIndex.value = index;
  showDetailModal.value = true;
};

// 关闭详情
const closeDetail = () => {
  showDetailModal.value = false;
  selectedRow.value = null;
  selectedIndex.value = -1;
};
</script>

<style scoped lang="scss">
.custom-table-wrapper {
  width: 100%;
  border-radius: 2px;
  overflow: hidden;

  .table-scroll-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    /* 移除滚动条相关设置 */
    /* overflow-x: auto; */
    /* -webkit-overflow-scrolling: touch; */
    /* Smooth scrolling on iOS */

    /* 移除自定义滚动条样式 */
    /* &::-webkit-scrollbar {
      height: 8px;
    }

    &::-webkit-scrollbar-track {
      background: rgba(0, 0, 0, 0.1);
      border-radius: 4px;
    }

    &::-webkit-scrollbar-thumb {
      background: rgba(53, 129, 242, 0.6);
      border-radius: 4px;

      &:hover {
        background: rgba(53, 129, 242, 0.8);
      }
    } */
  }

  .table-header {
    display: flex;
    height: 38px;
    background: rgba(191, 204, 224, 0.3);
    min-width: max-content;
    border-radius: 6px;
    opacity: 0.9;
    /* 确保表头不会被压缩 */

    .header-cell {
      padding: 16px 12px;
      font-weight: 600;
      color: #383C42;
      font-size: 14px;
      border-right: 1px solid rgba(255, 255, 255, 0.1);
      flex-shrink: 0;
      font-weight: 400;
      font-size: 14px;
      color: #3B567F;
      /* 防止列被压缩 */
      display: flex;
      align-items: center;
      box-sizing: border-box;
      /* 确保与表体单元格完全一致 */
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;

      &:last-child {
        border-right: none;
      }
    }
  }

  .table-body {
    position: relative;
    min-height: 120px;
    min-width: max-content;
    /* 确保表格内容不会被压缩 */

    .loading-wrapper {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      height: 120px;
      color: #606266;

      .loading-spinner {
        width: 32px;
        height: 32px;
        border: 3px solid rgba(53, 129, 242, 0.2);
        border-top: 3px solid #3581F2;
        border-radius: 50%;
        animation: spin 1s linear infinite;
        margin-bottom: 12px;
      }

      .loading-text {
        font-size: 14px;
        color: #909399;
      }
    }

    .table-row {
      display: flex;
      height: 35px;
      border-bottom: 1px solid rgba(255, 255, 255, 0.1);
      transition: all 0.3s ease;
      min-width: max-content;
      /* 确保行内容不会被压缩 */

      &:last-child {
        border-bottom: none;
      }

      // 偶数行使用半透明灰色背景
      &:nth-child(even) {
        background: rgba(224, 232, 244, 0.6);
      }

      // 奇数行保持纯透明
      &:nth-child(odd) {
        background: transparent;
      }

      &.row-hover {
        cursor: pointer;
        background: rgba(128, 128, 128, 0.1);
      }

      .table-cell {
        padding: 0 12px;
        color: #525A66;
        font-size: 14px;
        border-right: 1px solid rgba(255, 255, 255, 0.1);
        display: flex;
        align-items: center;
        flex-shrink: 0;
        /* 防止单元格被压缩 */
        // 添加文字超出省略号样式
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-weight: 400;
        font-size: 14px;
        color: #3B567F;
        box-sizing: border-box;

        &:last-child {
          border-right: none;
        }
      }
    }
  }
}

// Loading 动画
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }

  100% {
    transform: rotate(360deg);
  }
}

// 详情弹出层样式
.detail-modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.detail-modal {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow: hidden;
  animation: modalSlideIn 0.3s ease-out;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  background: linear-gradient(135deg, #3581F2, #5B9CF8);
  color: white;

  h3 {
    margin: 0;
    font-size: 18px;
    font-weight: 600;
  }

  .close-btn {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background-color 0.2s;

    &:hover {
      background: rgba(255, 255, 255, 0.2);
    }
  }
}

.detail-content {
  padding: 24px;
  max-height: 60vh;
  overflow-y: auto;

  .default-detail {
    .detail-item {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 12px 0;
      border-bottom: 1px solid rgba(0, 0, 0, 0.1);

      &:last-child {
        border-bottom: none;
      }

      .detail-label {
        font-weight: 600;
        color: #3B567F;
        font-size: 14px;
      }

      .detail-value {
        color: #525A66;
        font-size: 14px;
        text-align: right;
        max-width: 200px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }
}

// 弹出动画
@keyframes modalSlideIn {
  from {
    opacity: 0;
    transform: translateY(-20px) scale(0.95);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
</style>
